package com.ybfenpan.controller.member.center;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ybfenpan.controller.core.BaseController;
import com.ybfenpan.model.Member;
import com.ybfenpan.model.center.RequestCenter;
import com.ybfenpan.service.MemberService;
import com.ybfenpan.service.center.RequestCenterService;
import com.ybfenpan.utils.ContextUtil;
import com.ybfenpan.utils.CryptUtil;
import com.ybfenpan.utils.MapUtil;

/**
 * Created by Administrator on 2016/3/25.
 */
@Controller
public class RequestCenterController extends BaseController {

	@Resource
	private RequestCenterService requestCenterService;

	@Resource
	private MemberService memberService;

	/**
	 * 报单显示
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("member/center/requestCenterShow.shtml")
	public String show(HttpServletRequest request) throws Exception {

		Member one = ContextUtil.getMember();
		System.out.println("-------one-------" + one);
		request.setAttribute("one", one);
		return "member/center/requestCenterAdd";

	}

	@RequestMapping("member/center/requestCenterAdd.shtml")
	public String add(ModelMap model, HttpServletRequest request, HttpServletResponse response) throws Exception {

		Member member = ContextUtil.getMember();
		Integer memberId = member.getId();

		Map formMap = getParameterMap(request);

		String twoPassword = MapUtil.getString(formMap, "myTwoPassword");
		String pwd = CryptUtil.getInstance().encrypt2String(twoPassword, CryptUtil.keyStr);
		Integer centerLevel=MapUtil.getInteger(formMap,"centerLevel");
		Boolean flag = true;
		if (!member.getTwoPassword().equals(pwd)) {

			setMessage(model, "安全密码不正确。");
			flag = false;

			Member one = ContextUtil.getMember();
			request.setAttribute("one", one);
			return "member/center/requestCenterAdd";

		} else {
			try {
				//
				// 新增
				String title = MapUtil.getString(formMap, "title");
				String address = MapUtil.getString(formMap, "address");

				if (title == null || title.equals("")) {
					setMessage(model, "报单中心名称没有填写");
					flag = false;
					Member one = ContextUtil.getMember();
					request.setAttribute("one", one);
					return "member/center/requestCenterAdd";

				}
				if (address == null || address.equals("")) {
					setMessage(model, "报单中心地址没有填写");
					flag = false;
					Member one = ContextUtil.getMember();
					request.setAttribute("one", one);
					return "member/center/requestCenterAdd";
				}
				if (member.getAccountCenter().equals("T")) {
					setMessage(model, "您已经是区域管理中心了，请勿重复申请！");
					flag = false;
					Member one = ContextUtil.getMember();
					request.setAttribute("one", one);
					return "member/center/requestCenterAdd";
				}
				// 获取list
				Map reMap = getParameterMap(request);
				reMap.put("title", ""); // 启用的
				reMap.put("address", ""); // 启用的
				reMap.put("where", " memberId=" + memberId + " and state=10");
				List reList = requestCenterService.queryForList("select", reMap);

				if (reList.size() > 0) {
					setMessage(model, "您已经申请过了,请等待审核");
					flag = false;
					Member one = ContextUtil.getMember();
					request.setAttribute("one", one);
					return "member/center/requestCenterAdd";
				}

				if (flag) {
					RequestCenter shang = new RequestCenter();
					shang.setMemberId(memberId);
					shang.setCenterLevel(centerLevel);
					shang.setTitle(title);
					shang.setAddress(address);
					this.requestCenterService.saveOrUpdate(shang);
					return "redirect:memberRequestCenterList.shtml";
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}

		return "redirect:memberRequestCenterList.shtml";

	}

}
